package chapter7.No7_4_SimpleDateFormat非线程安全.c解决异常方法2;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Administrator on 2016/12/8.
 */
public class MyThread extends Thread {
    String dateString;
    String pattern;

    public MyThread(String pattern, String dateString) {
        this.pattern = pattern;
        this.dateString = dateString;
    }

    MyDateThreadLocal<SimpleDateFormat> sdfTL = new MyDateThreadLocal<SimpleDateFormat>();

    @Override
    public void run() {
        SimpleDateFormat sdf = (SimpleDateFormat) sdfTL.get();
        sdf.applyPattern(pattern);
        try {
            Date date = sdf.parse(dateString);
            String newString = sdf.format(date);
            if (!newString.equals(dateString)) {
                System.out.println("线程：" + Thread.currentThread().getName() +
                        " 程序出现转换错误,原值：" + dateString + " 转换后的值：" + newString);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}
